package game_frame

import (
	"gamesdk/common/log"

	"github.com/golang/protobuf/proto"
)

const (
	g_ChairFree  = 0 //椅子空闲
	g_ChairDown  = 1 //椅子做下
	g_ChairReady = 2 //椅子准备
	g_ChairRun   = 3 //椅子正在游戏
	g_ChairEnd   = 4 //椅子游戏结束
)

type Chair struct {
	number int //编号
	status int //状态
	user   *User
}

func NewChair(num int) *Chair {
	defer log.Trace()()
	return &Chair{
		number: num,
		status: g_ChairFree,
		user:   nil,
	}
}

/*
*获取编号
 */
func (this *Chair) GetNum() int {
	defer log.Trace()()
	return this.number
}

/*
*获取用户
 */
func (this *Chair) GetUser() *User {
	defer log.Trace()()
	return this.user
}

func (this *Chair) GetStatus() int {
	defer log.Trace()()
	return this.status
}

/*
*用户坐下
 */
func (this *Chair) SetDown(user *User) {
	defer log.Trace()()
	this.user = user
	this.status = g_ChairDown
	this.user.SetDown()
}

/*
*用户准备
 */
func (this *Chair) Ready() {
	defer log.Trace()()
	if this.user != nil {
		this.status = g_ChairReady
		this.user.Ready()
	}
}

/*
*游戏中
 */
func (this *Chair) GameRun(gameNum string) {
	defer log.Trace()()
	if this.user != nil {
		this.status = g_ChairRun
		//log.Debug("game end %v", this.user)
		this.user.GameRun(gameNum)
	}
}

/*
*游戏结束
 */
func (this *Chair) GameEnd(gameNum string) {
	defer log.Trace()()
	if this.user != nil {
		this.status = g_ChairEnd
		this.user.GameEnd(gameNum)
	}
}

/*
*用户退出
 */
func (this *Chair) Leave() {
	defer log.Trace()()
	if this.user != nil {
		this.user.Leave()
		this.status = g_ChairFree
	}
	this.user = nil
}

/*
*用户踢人
 */
func (this *Chair) KickOut() {
	defer log.Trace()()
	if this.user != nil {
		this.user.Leave()
		this.user = nil
		this.status = g_ChairFree
	}
}

func (this *Chair) SendMsg(subCmd int32, pb proto.Message) {
	defer log.Trace()()
	if this.user != nil {
		this.user.SendMsg(subCmd, pb)
	}
}

func (this *Chair) sendMsgF(subCmd int32, buff []byte) {
	defer log.Trace()()
	if this.user != nil {
		this.user.sendMsgF(subCmd, buff)
	}
}
